package com.swq.java.code.chapter11.code14;

import java.util.EmptyStackException;

public class Stack {
    private Object[] elements;  //存放对象
    private int size = 0;
    private int capacityIncrement = 10;  //堆栈的容量增长的步长

    public Stack(int initialCapacity, int capacityIncrement) {
        this(initialCapacity);
        this.capacityIncrement = capacityIncrement;
    }

    public Stack(int initialCapacity) {
        elements = new Object[initialCapacity];
    }

    public void push(Object object) {
        ensureCapacity();
        elements[size++] = object;
    }

    public Object pop() {
        if (size == 0)
            throw new EmptyStackException();

        Object object = elements[--size];
        elements[size] = null;  //清除数组中已经出栈的对象引用
        return object;
    }

    private void ensureCapacity() {  //增加堆栈的容量
        if (elements.length == size) {
            Object[] oldElements = elements;
            elements = new Object[elements.length + capacityIncrement];
            System.arraycopy(oldElements, 0, elements, 0, size);
        }
    }

    public static void main(String args[]) {
        Stack stack = new Stack(1000);
        for (int a = 0; a < 1000; a++)
            stack.push(new Integer(a));
        for (int a = 0; a < 1000; a++)
            System.out.println(stack.pop());
    }
}